import { Module } from 'vuex'
import { Waybill } from './types'
import GlobalDataProps from '../types'
import { PagingModel } from '@/store/baseTypes'
import http from '@/http/index'
import { api } from '@/http/api'

const waybill: Module<Waybill, GlobalDataProps> = {
  namespaced: true,
  state: {
    isRefresh: false, //运单列表是否当前条件下重新刷新数据
    isNextTab: false //运单详情确认签收返回列表页跳转下一个tab项
  },
  getters: {},
  mutations: {
    updateRefresh: (state, val: boolean): void => {
      state.isRefresh = val
    },
    updateNextTab: (state): void => {
      state.isNextTab = !state.isNextTab
    }
  },
  actions: {
    // 获取运单列表
    handleGetWaybillList: async ({ commit }, params: any): Promise<PagingModel<any>> => {
      let finalResult: PagingModel<any> = {} as PagingModel<any>
      try {
        const result = await http({
          url: api.getWaybillList,
          params,
          method: 'get'
        })
        finalResult = result
      } catch (e) {
        console.log(`ERROR FROM handleGetWaybillList: ${e}`)
      }
      return finalResult
    },
    // 获取运单详情
    handleGetWaybillDetail: async ({ commit }, data: any): Promise<any> => {
      let finalResult: any = {}
      try {
        const result = await http({
          url: api.getWaybillDetail,
          data,
          method: 'post'
        })
        finalResult = result
      } catch (e) {
        console.log(`ERROR FROM handleGetWaybillDetail: ${e}`)
      }
      return finalResult
    },
    // 订单运单列表
    handleGetWaybillAllList: async ({ commit }, params: any): Promise<PagingModel<any>> => {
      let finalResult: PagingModel<any> = {} as PagingModel<any>
      console.log(params, 'dd')

      try {
        const result = await http({
          url: api.getWaybillAllList,
          params,
          method: 'get'
        })
        finalResult = result
      } catch (e) {
        console.log(`ERROR FROM handleGetWaybillAllList: ${e}`)
      }
      return finalResult
    },
    // 获取订单列表
    handleGetOrderList: async ({ commit }, params: any): Promise<PagingModel<any>> => {
      let finalResult: PagingModel<any> = {} as PagingModel<any>
      try {
        const result = await http({
          url: api.getOrderList,
          params,
          method: 'get'
        })
        finalResult = result
      } catch (e) {
        console.log(`ERROR FROM handleGetOrderList: ${e}`)
      }
      return finalResult
    },
    // 查看单据
    handleTransportBillGetReceipt: async ({ commit }, data: any): Promise<any> => {
      let finalResult: any = {}
      try {
        const result = await http({
          url: api.transportBillGetReceipt,
          data,
          method: 'post'
        })
        finalResult = result
      } catch (e) {
        console.log(`ERROR FROM handleTransportBillGetReceipt: ${e}`)
      }
      return finalResult
    },
    // 确认单据
    handleTransportBillConfirmCons: async ({ commit }, data): Promise<boolean> => {
      let finalResult = false
      try {
        await http({
          url: api.transportBillConfirmCons,
          data,
          method: 'post'
        })
        finalResult = true
      } catch (e) {
        console.log(`ERROR FROM handleTransportBillConfirmCons: ${e}`)
      }
      return finalResult
    },
    // 单据驳回
    handleTransportBillRejectReceipt: async ({ commit }, data): Promise<boolean> => {
      let finalResult = false
      try {
        await http({
          url: api.transportBillRejectReceipt,
          data,
          method: 'post'
        })
        finalResult = true
      } catch (e) {
        console.log(`ERROR FROM handleTransportBillRejectReceipt: ${e}`)
      }
      return finalResult
    }
  }
}
export default waybill
